<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Minecraft Proxy (e6p3f2) Web Control Panel</title>
    <script type="text/javascript" src="jquery.js"></script>
    
	<style type="text/css" media="screen">
	* { font-size: 10pt }
	body { background: #aaa }
	#wrap {
		width: 1300px;
		border: none;
		margin: 20px auto;
		border-spacing: 6px;
	}
	td.section {
		background: #fff;
		border: 1px solid #000;
		padding: 10px;
		vertical-align: text-top;
	}
	#left-panel { width: 500px }
	#chat-log, #sys-log { 
		overflow: auto;
		width: 100%;
		height: 400px; 
		font-family: monospace;
		font-size: 9pt;
		white-space: pre-wrap; /* css-3 */
		white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
		white-space: -pre-wrap; /* Opera 4-6 */
		white-space: -o-pre-wrap; /* Opera 7 */
		word-wrap: break-word; /* Internet Explorer 5.5+ */
	}
	.small-text { width: 100px; }
	.large-text { width: 450px; }
	#console { width: 550px; }
	.inner-panel {
		border: 1px solid #888;
		padding: 6px;
		margin: 6px;
		text-align: right;
	}
	</style>
	
	<script type="text/javascript">
	var last_rec = {};
	last_rec['chat'] = -2;
	last_rec['log'] = -2;

	var poll_other = false;
	var polling = false;
	
	function get_log(name, dest) {
		if (last_rec[name] == -2) return;
		
		var requrl = name + '/' + (last_rec[name] + 1) + '/100';

		$.post(requrl, {}, function(ret) {
			if (!polling) return;
			
			var lnpos = ret.indexOf("\n");
			var returned = parseInt(ret.substring(0, lnpos));

			if (returned > 0) {
				var logs = ret.substring(lnpos + 1);
	
				last_rec[name] = returned;
				dest.append(logs);
				dest[0].scrollTop = dest[0].scrollHeight;
			}
			
			polling = false;
		});
	}

	function log_poll() {
		if (polling) return;
		polling = true;
		if (poll_other) {
			poll_other = false;
			get_log('chat', $('#chat-log'));
		} else {
			poll_other = true;
			get_log('log', $('#sys-log'));
		} 
	}

	function console_enter() {
        $.get('cmd/' + $('#console').val(), {});
        $('#console').val('');
	}

	function chat_enter() {
        $.get('cmd/say [' + $('#chat-name').val() + '] ' + $('#chat-content').val(), {});
        $('#chat-content').val('');
	}

	$(document).ready(function() {
		setInterval("log_poll()", 2000);
		setInterval("give_step()", 500);

		$(document).ajaxError(function(event, request, settings) {
			polling = false;
		});
		// Chat action
		$('#chat-content').keypress(function(e) {
		    if(e.keyCode == 13) chat_enter();
		});
		// Console action
		$('#console').keypress(function(e) {
		    if(e.keyCode == 13) console_enter();
		});
		// Get the length of the console log.
		// Retrieve only the last 100 lines of history
		$.post('log/last', {}, function(ret) {
			last_rec['log'] = parseInt(ret) - 100;
			if (last_rec['log'] < 0) last_rec['log'] = -1;
			$('#sys-log').append('Retriving logs from line #' + (last_rec['log'] + 1) + "\n");
		});
		// Same thing for the chat logs
		$.post('chat/last', {}, function(ret) {
			last_rec['chat'] = parseInt(ret) - 100;
			if (last_rec['chat'] < 0) last_rec['chat'] = -1;
			$('#chat-log').append('Retriving chat logs from line #' + (last_rec['chat'] + 1) + "\n");
		});
	});

	function restart() {
		if (confirm('Are you sure you want to start or restart a Minecraft server instance?')) {
			$.get('restart', {});
		}
	}

	function stop() {
		if (confirm('Are you sure you want to stop the Minecraft server instance?')) {
			$.get('stop', {});
		}
	}

	function shutdown() {
		if (confirm('Are you sure you want to TERMINATE the Minecraft proxy server? You will not be able to use this control panel after doing so until the proxy server is restarted')) {
			$.get('shutdown', {}, function(ret) { alert(ret); });
		}		
	}

	function backup() {
		if (confirm('Are you sure you want to backup the world now? This will stop the Minecraft server if it is running.')) {
			$.get('backup', {});
		}
	}

	var give_lock = false;
	var give_processing_lock = false;
	var give_amount = 0;
	var give_cmd = '';

	function give() {
		if (give_processing_lock || give_lock) {
			alert('Wait... I\'m still processing your last request...');
			return;
		}

		give_processing_lock = true;

		give_amount = parseInt($('#give-amount option:selected').val());
		var what = parseInt($('#give-what option:selected').val());
		var who = $('#give-name').val();

		if (what == '-1') what = parseInt($('input[name=customcode]').val());

		give_cmd = 'cmd/give ' + who + ' ' + what + ' ';
		give_lock = true;
	}

	function give_step() {
		if (!give_lock) return;
		if (give_amount == 0) {
			give_lock = false;
			give_processing_lock = false;
			return;
		}

		var cur_amt;

		if (give_amount > 64) {
			cur_amt = 64;
			give_amount -= 64;
		} else {
			cur_amt = give_amount;
			give_amount = 0;
		}

		$.get(give_cmd + cur_amt, {});
	}
	</script>
</head><body><table id="wrap"><tr>
<td rowspan="2" class="section" id="left-panel">
<div>
	<input type="submit" value="Start/Restart Minecraft" onclick="javascript: restart(); return false" />
	<input type="submit" value="Stop Minecraft" onclick="javascript: stop(); return false" />
	<input type="submit" value="Shutdown Proxy" onclick="javascript: shutdown(); return false" />
	<input type="submit" value="Backup" onclick="javascript: backup(); return false" />
</div><br /><div class="inner-panel">
	Give to <input type="text" id="give-name" class="small-text" value="$user" />
	<select id="give-amount">
		<option value="1">1</option>
		<option value="6">6</option>
		<option value="16">16</option>
		<option value="32">32</option>
		<option value="64" selected>1 stack</option>
		<option value="128">2 stacks</option>
		<option value="256">4 stacks</option>
		<option value="576">9 stacks</option>
		<option value="3456">54 stacks</option>
	</select>
	of
	<select id="give-what">
		<option value="262">Arrow</option>
		<option value="355">Bed</option>
		<option value="333">Boat</option>
		<option value="352">Bone</option>
		<option value="261">Bow</option>
		<option value="325">Bucket</option>
		<option value="82">Clay</option>
		<option value="347">Clock</option>
		<option value="263">Coal</option>
		<option value="4">Cobblestone</option>
		<option value="345">Compass</option>
		<option value="28">Detector Rail</option>
		<option value="264">Diamond</option>
		<option value="3">Dirt</option>
		<option value="288">Feather</option>
		<option value="85">Fence</option>
		<option value="318">Flint</option>
		<option value="20">Glass</option>
		<option value="266">Gold Ingot</option>
		<option value="13">Gravel</option>
		<option value="289">Gunpowder</option>
		<option value="265">Iron Ingot</option>
		<option value="65">Ladder</option>
		<option value="327">Lava Bucket</option>
		<option value="335">Milk Bucket</option>
		<option value="328">Minecart</option>
		<option value="27">Powered Rail</option>
		<option value="86">Pumpkin</option>
		<option value="66">Rails</option>
		<option value="331">Redstone</option>
		<option value="12">Sand</option>
		<option value="323">Sign</option>
		<option value="1">Stone</option>
		<option value="50">Torch</option>
		<option value="326">Water Bucket</option>
		<option value="296">Wheat</option>
		<option value="17">Wood</option>
		<option value="-1">Custom</option>
	</select>
	<input type="text" name="customcode" style="width: 30px" />
	<br />
	<input style="margin-top: 6px" type="submit" value="Give!" onclick="javascript: give(); return false" />
</div><div class="inner-panel" style="text-align: center">
	<a href="download/backups/">Download server backups</a>
</div>
</td><td class="section" id="log-panel">
System log:<br />
<pre id="sys-log"></pre>
<input type="text" id="console" /><input type="submit" value="Enter" onclick="javascript: console_enter(); return false" />
</td></tr><tr><td class="section" id="chat-panel">
Chat History:<br />
<pre id="chat-log"></pre>
<input type="text" id="chat-name" class="small-text" value="$user" />
<input type="text" id="chat-content" class="large-text" />
<input type="submit" value="Send" onclick="javascript: chat_enter(); return false" />
</td></tr></table></body></html>
